package com.ciwong.epaper.modules.pad.homeworkwidget.common.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class SDCardBitmap {

	public static Bitmap getBitmapFromSD(String key, Context context) {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			Bitmap map = BitmapFactory.decodeFile(getFilePath(context, key));
			return map;
		}
		return null;
	}

	public static void setBitmapToSD(String key, Bitmap map, Context context) {

		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			File file;
			file = new File(getFilePath(context, key));
			FileOutputStream outputStream = null;
			try {
				file.createNewFile();
				outputStream = new FileOutputStream(file);

				map.compress(CompressFormat.JPEG, 100, outputStream);
				outputStream.flush();

			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		}
	}

	private static String getFilePath(Context context, String key) {
		String cachePath = null;
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable()) {
			File externalCacheDir = context.getExternalCacheDir();
			if (externalCacheDir!=null)
				cachePath = externalCacheDir.getAbsolutePath()
					+ File.separator + key.substring(key.lastIndexOf("/") + 1);
		} else {
			cachePath = context.getCacheDir().getAbsolutePath()
					+ File.separator + key.substring(key.lastIndexOf("/") + 1);
		}
		return cachePath;
	}
}
